package com.ming.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Log extends JFrame implements MouseListener {

    public static ArrayList<User> allUsers = new ArrayList<>();
    static {
        allUsers.add(new User("zdm","123"));
    }
    JButton login = new JButton();
    //登录按钮
    JButton hideButton = new JButton();
    //隐藏按钮
    JButton visibleButton = new JButton();
    JButton register = new JButton();
    //注册按钮
    //使用JTextField输入框
    JTextField username = new JTextField();
    //输入用户名
    TextField password = new TextField();
    //输入密码
    JTextField code = new JTextField();
    //输入验证码

    //正确的验证码
    JLabel rightCode = new JLabel();
    //与登录有关的内容写在此处
    public Log(ArrayList<User> allUsers) {
        //初始化界面
        initJFrame();
        initView();
        this.setVisible(true);
    }
    private void initJFrame() {
        //游戏进行时的画面
        //设置画面的宽和高
        this.setSize(488,430);

        //设置界面的标题
        this.setTitle("拼图 V1.0登录界面  张德明最帅");

        //设置界面始终置顶
        this.setAlwaysOnTop(true);

        //设置界面居中
        this.setLocationRelativeTo(null);

        //取消默认的居中方式
        this.setLayout(null);

        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public void initView(){

        //1.用户名文字
        JLabel accountTest = new JLabel(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\用户名.png"));
        accountTest.setBounds(116,135,47,17);
        this.getContentPane().add(accountTest);


        //2.用户名输入框
        username.setBounds(195,134,200,30);
        this.getContentPane().add(username);


        //3.密码文字
        JLabel passwordText = new JLabel(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\密码.png"));
        passwordText.setBounds(130,195,32,16);
        this.getContentPane().add(passwordText);


        //4.密码输入框
        password.setBounds(195,195,200,30);
        this.getContentPane().add(password);

        //隐藏
        hideButton.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\显示密码.png"));
        hideButton.setBounds(400,195,18,29);
        hideButton.setBorderPainted(false);
        //去除注册的背景
        hideButton.setContentAreaFilled(false);
        //绑定鼠标事件
        hideButton.addMouseListener(this);
        //添加按钮
        this.getContentPane().add(hideButton);


        //显示
        visibleButton.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\显示密码按下.png"));
        visibleButton.setBounds(400,195,21,32);
        visibleButton.setBorderPainted(false);
        //去除注册的背景
        visibleButton.setContentAreaFilled(false);
        //绑定鼠标事件
        visibleButton.addMouseListener(this);
        //添加按钮
        this.getContentPane().add(visibleButton);
        visibleButton.setVisible(false);


        //验证码提示
        JLabel codeText = new JLabel(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\验证码.png"));
        codeText.setBounds(133,256,50,30);
        this.getContentPane().add(codeText);

        //验证码输入
        code.setBounds(195,256,100,30);
        this.getContentPane().add(code);

        String codeStr = CodeUntil.getCode();
        //设置内容
        rightCode.setText(codeStr);
        //绑定鼠标事件
        rightCode.addMouseListener(this);
        //位置和宽高
        rightCode.setBounds(300,256,50,30);
        this.getContentPane().add(rightCode);


        //5.添加登录按钮
        login.setBounds(123,310,128,47);
        login.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\登录按钮.png"));
        //去除登录的边框
        login.setBorderPainted(false);
        //去除登录的背景
        login.setContentAreaFilled(false);
        //绑定鼠标事件
        login.addMouseListener(this);
        this.getContentPane().add(login);

        //6.添加注册按钮
        register.setBounds(256,310,128,47);
        register.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\注册按钮.png"));
        //去除注册的边框
        register.setBorderPainted(false);
        //去除注册的背景
        register.setContentAreaFilled(false);
        //绑定鼠标事件
        register.addMouseListener(this);
        this.getContentPane().add(register);

        //添加背景
        JLabel background = new JLabel(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\background.png"));
        background.setBounds(0,0,470,390);
        this.getContentPane().add(background);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if(source == login){
            System.out.println("点击了登录按钮");
            //获取两个文本的内容
            String usernameInput = username.getText();
            String passwordInput = password.getText();
            //获取输入的验证码
            String codeInput = code.getText();

            //创建一个user对象
            User userInfo = new User(usernameInput,passwordInput);
            System.out.println("用户输入的用户名为" + usernameInput);
            System.out.println("用户输入的密码为" + passwordInput);

            if(codeInput.length() == 0){
                showJDialog("验证码不能为空");
            } else if (usernameInput.length() == 0 || passwordInput.length() == 0) {
                System.out.println("用户名或者密码为空");
                showJDialog("用户名或者密码为空");
            } else if (!codeInput.equalsIgnoreCase(rightCode.getText())) {
                showJDialog("验证码输入错误");
                
            } else if (contains(userInfo)) {
                showJDialog("登录成功");
                //关闭登录界面
                this.setVisible(false);
                if (usernameInput.equals("houyaxin")){
                    showJDialog("候雅欣大傻子");
                } else if (usernameInput.equals("zdm")) {
                    showJDialog("张德明大帅哥");
                }
                //打开游戏界面
                new Game();
            }else {
                showJDialog("用户名或密码错误");
            }

        } else if (e.getSource() == rightCode) {
            System.out.println("更换验证码");
            //获取新的验证码
            String newCode = CodeUntil.getCode();
            rightCode.setText(newCode);
        } else if (e.getSource() == register) {
            System.out.println("点击了注册按钮");
            this.setVisible(false);
            new Register(allUsers);
        } else if (e.getSource() == hideButton) {
            System.out.println("点击了隐藏按钮");
            hideButton.setVisible(false);
            visibleButton.setVisible(true);
            password.setEchoChar('*');
        } else if (e.getSource() == visibleButton) {
            System.out.println("点击了显示按钮");
            visibleButton.setVisible(false);
            hideButton.setVisible(true);
            password.setEchoChar((char) 0);
        }
    }
    public void showJDialog(String content){
        //创建一个弹窗对象
        JDialog jDialog = new JDialog();
        //设置大小
        jDialog.setSize(200,150);
        //弹框置顶
        jDialog.setAlwaysOnTop(true);
        //居中
        jDialog.setLocationRelativeTo(null);
        //不关闭无法进行其他的操作
        jDialog.setModal(true);
        
        //创建JLabel对象
        JLabel warning = new JLabel(content);
        warning.setBounds(0,0,200,150);
        jDialog.getContentPane().add(warning);
        
        //展示
        jDialog.setVisible(true);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getSource() == login){
            login.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\登录按下.png"));
        } else if (e.getSource() == register) {
            register.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\注册按下.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getSource() == login) {
            login.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\登录按钮.png"));
        } else if (e.getSource() == register) {
            register.setIcon(new ImageIcon("C:\\Users\\Zdm\\IdeaProjects\\Main\\PuzzleGame\\image\\login\\注册按钮.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
    public boolean contains(User userInput){
        for (int i = 0; i < allUsers.size(); i++) {
            User rightUser = allUsers.get(i);
            if(userInput.getUsername().equals(rightUser.getUsername()) 
            &&userInput.getPassword().equals(rightUser.getPassword())){
                return true;
            }
        }
        return false;
    }


}
